package com.qzq.weibo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qzq.weibo.R;
import com.qzq.weibo.fragment.DiscoverFragment;
import com.qzq.weibo.fragment.HomeFragment;
import com.qzq.weibo.fragment.MessageFragment;
import com.qzq.weibo.fragment.ProfileFragment;
import com.qzq.weibo.utils.Utils;

public class MainActivity extends FragmentActivity implements OnClickListener {

	private Utils utils;

	private int nor_color;
	private int selected_color;

	private LinearLayout ll_tabbar;

	private LinearLayout tab_home;
	private LinearLayout tab_message;
//	private LinearLayout tab_discover;
	private LinearLayout tab_profile;

	private ImageView iv_home;
	private ImageView iv_message;
//	private ImageView iv_discover;
	private ImageView iv_profile;

	private TextView tv_home;
	private TextView tv_message;
//	private TextView tv_discover;
	private TextView tv_profile;

	/** 按下时记录的Y坐标 */
	private int downPositionY;
	/** 手指滑动时的当前Y坐标 */
	private int currentPositionY;
	/** 偏移量 ， 正为下拉 ， 负为上拉 */
	private int pullDistance;
	/** tabbar是否显示 */
	private boolean isShowTabBar;
	/** 动画所需的时间 */
	private static final int ANIM_TIME = 150;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		initMember();

		onClickTabHome();
	}

	private void initView() {

		this.ll_tabbar = (LinearLayout) this.findViewById(R.id.main_ll_tabbar);
		this.tab_home = (LinearLayout) this.findViewById(R.id.main_ll_tab_home);
		this.tab_message = (LinearLayout) this
				.findViewById(R.id.main_ll_tab_message);
//		this.tab_discover = (LinearLayout) this
//				.findViewById(R.id.main_ll_tab_discover);
		this.tab_profile = (LinearLayout) this
				.findViewById(R.id.main_ll_tab_profile);

		this.iv_home = (ImageView) this.findViewById(R.id.main_iv_tab_home);
		this.iv_message = (ImageView) this
				.findViewById(R.id.main_iv_tab_message);
//		this.iv_discover = (ImageView) this
//				.findViewById(R.id.main_iv_tab_discover);
		this.iv_profile = (ImageView) this
				.findViewById(R.id.main_iv_tab_profile);

		this.tv_home = (TextView) this.findViewById(R.id.main_tv_tab_home);
		this.tv_message = (TextView) this
				.findViewById(R.id.main_tv_tab_message);
//		this.tv_discover = (TextView) this
//				.findViewById(R.id.main_tv_tab_discover);
		this.tv_profile = (TextView) this
				.findViewById(R.id.main_tv_tab_profile);

	}

	private void initMember() {
		utils = Utils.getInstance(getApplicationContext());
		tab_home.setOnClickListener(this);
		tab_message.setOnClickListener(this);
//		tab_discover.setOnClickListener(this);
		tab_profile.setOnClickListener(this);

		nor_color = getResources().getColor(R.color.tab_txt_color_nor);
		selected_color = getResources()
				.getColor(R.color.tab_txt_color_selected);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.main_ll_tab_home:
			onClickTabHome();
			break;

		case R.id.main_ll_tab_message:
			onClickTabMessage();
			break;

//		case R.id.main_ll_tab_discover:
//			onClickTabDiscover();
//			break;

		case R.id.main_ll_tab_profile:
			onClickTabProfile();
			break;

		default:
			break;
		}

	}

	private void onClickTabHome() {
		FragmentTransaction ft = this.getSupportFragmentManager()
				.beginTransaction();
		ft.replace(R.id.frame_content, HomeFragment.getInstance());
		ft.commit();

		iv_home.setSelected(true);
		tv_home.setTextColor(selected_color);

		iv_message.setSelected(false);
		tv_message.setTextColor(nor_color);

//		iv_discover.setSelected(false);
//		tv_discover.setTextColor(nor_color);

		iv_profile.setSelected(false);
		tv_profile.setTextColor(nor_color);
	}

	private void onClickTabMessage() {
		FragmentTransaction ft = this.getSupportFragmentManager()
				.beginTransaction();
		ft.replace(R.id.frame_content, MessageFragment.getInstance());
		ft.commit();

		iv_home.setSelected(false);
		tv_home.setTextColor(nor_color);

		iv_message.setSelected(true);
		tv_message.setTextColor(selected_color);

//		iv_discover.setSelected(false);
//		tv_discover.setTextColor(nor_color);

		iv_profile.setSelected(false);
		tv_profile.setTextColor(nor_color);
	}

//	private void onClickTabDiscover() {
//		FragmentTransaction ft = this.getSupportFragmentManager()
//				.beginTransaction();
//		ft.replace(R.id.frame_content, DiscoverFragment.getInstance());
//		ft.commit();
//
//		iv_home.setSelected(false);
//		tv_home.setTextColor(nor_color);
//
//		iv_message.setSelected(false);
//		tv_message.setTextColor(nor_color);
//
//		iv_discover.setSelected(true);
//		tv_discover.setTextColor(selected_color);
//
//		iv_profile.setSelected(false);
//		tv_profile.setTextColor(nor_color);
//	}

	private void onClickTabProfile() {
		FragmentTransaction ft = this.getSupportFragmentManager()
				.beginTransaction();
		ft.replace(R.id.frame_content, ProfileFragment.getInstance());
		ft.commit();

		iv_home.setSelected(false);
		tv_home.setTextColor(nor_color);

		iv_message.setSelected(false);
		tv_message.setTextColor(nor_color);

//		iv_discover.setSelected(false);
//		tv_discover.setTextColor(nor_color);

		iv_profile.setSelected(true);
		tv_profile.setTextColor(selected_color);
	}

	/** 触摸事件:动态显示||隐藏Bar */
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

		int action = ev.getAction();
		int y = (int) ev.getRawY();

		switch (action) {
		case MotionEvent.ACTION_UP:
			break;

		case MotionEvent.ACTION_DOWN:
			downPositionY = y;
			break;

		case MotionEvent.ACTION_MOVE:

			currentPositionY = y;
			pullDistance = (currentPositionY - downPositionY);

			if (pullDistance > 40) {
				isShowTabBar = true;
				updateTabBar();
			} else if (pullDistance < -40) {
				isShowTabBar = false;
				updateTabBar();
			}

			break;

		default:
			break;
		}

		return super.dispatchTouchEvent(ev);
	}

	private void updateTabBar() {

		if (isShowTabBar) {

			TranslateAnimation mShowAction = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 1.0f,
					Animation.RELATIVE_TO_SELF, 0.0f);

			mShowAction.setDuration(ANIM_TIME);
			ll_tabbar.setAnimation(mShowAction);
			ll_tabbar.setVisibility(View.VISIBLE);
		} else {

			TranslateAnimation mHiddenAction = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 1.0f);
			mHiddenAction.setDuration(ANIM_TIME);

			ll_tabbar.setAnimation(mHiddenAction);
			ll_tabbar.setVisibility(View.GONE);
		}

	}

	/** 双击back退出程序 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			utils.exitBy2Click(this);
		}
		return false;
	}

	/** 对返回结果码的执行 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 11) {
			onClickTabHome();
		}
	}
}
